package com.crr.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crr.mall.product.dao.AttrAttrgroupRelationDao;
import com.crr.mall.product.dao.AttrDao;
import com.crr.mall.product.entity.AttrAttrgroupRelationEntity;
import com.crr.mall.product.entity.AttrEntity;
import com.crr.mall.product.vo.AttrGroupRelationVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.product.dao.AttrGroupDao;
import com.crr.mall.product.entity.AttrGroupEntity;
import com.crr.mall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Autowired
    AttrDao attrDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByCondition(Map map) {
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.queryByCondition(map);
        PageInfo info = new PageInfo(list);
        return new PageUtils(list, (int) info.getTotal(), pageSize, page);
    }

    /**
     * 查询关联信息
     *
     * @param groupId
     * @return
     */
    @Override
    public List<Map> queryAttrGroupRelation(Long groupId) {
        return baseMapper.queryAttrGroupRelation(groupId);
    }


//    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
//        if(catelogId==0){
//            //没有指定分类id 则查询全部
//            IPage<AttrGroupEntity> page = this.page(
//                    new Query<AttrGroupEntity>().getPage(params),
//                    new QueryWrapper<AttrGroupEntity>()
//            );
//
//            return new PageUtils(page);
//        }else {
//            //params 集合中会传过来一个key属性用于检索条件 id匹配或者（分组名或描述包含有）
//            //select * from p_attr_group where catelogId=#{catelogId}
//            // and (attr_group_id=#{key} or attr_group_id like concat('%',#{key},'%')
//            // or descript like concat('%',#{key},'%')
//            return null;
//        }
//    }

    @Override
    public int deleteByids(AttrGroupRelationVO[] attrGroupVOS) {
        List<AttrAttrgroupRelationEntity> list =
                Arrays.stream(attrGroupVOS).map((item) -> {
                    AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                    BeanUtil.copyProperties(item, attrAttrgroupRelationEntity);
                    return attrAttrgroupRelationEntity;
                }).collect(Collectors.toList());
        return relationDao.deleteByids(list);
    }

    @Override
    public PageUtils queryNotRelation(Map map) {
        //根据attrGroupId 查询catelogid
        Long attrGroupId = (Long) map.get("attrGroupId");
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        //根据catelogId查询attrGroup分组列表信息
        List<Long> groupids = baseMapper.queryIdsByCatelogId(catelogId);
        //查询attr数据
        List<Map> list = attrDao.queryAttrList(groupids, catelogId);
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        PageInfo info = new PageInfo(list);
        return new PageUtils(list, (int) info.getTotal(), pageSize, page);
    }

    @Override
    public int saveGroupinfo(List<AttrGroupRelationVO> attrGroupVOS) {
        return relationDao.saveGroupinfo(attrGroupVOS);
    }


    @Override
    public List<AttrGroupEntity> queryGroupAndAttrByCatelogId(Long catelogId) {

//        List<AttrGroupEntity> list = baseMapper.queryGroupAndAttrByCatelogId(catelogId);
//        list.forEach(l -> {
//            Long groupId = l.getAttrGroupId();
//            List<AttrEntity> attrEntities = relationDao.queryAttrBygroupId(groupId);
//            l.setChildren(attrEntities);
//        });
//        return list;
        return baseMapper.queryGroupAndAttrByCatelogId(catelogId);
    }

}